﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Media;
using System.Collections;

namespace XWord.WPF.Behaviors
{
    public class AttachDayIndexBehavior : Behavior<ItemsControl>
    {
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
          
            base.OnAttached();
        }

        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(AssociatedObject)) return;
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.MarkIndex();
            }), DispatcherPriority.Render);
        }

        public IList Resources
        {
            get { return (IList)GetValue(ResourcesProperty); }
            set { SetValue(ResourcesProperty, value); }
        }

        public static readonly DependencyProperty ResourcesProperty =
            DependencyProperty.Register("Resources", typeof(IList), typeof(AttachDayIndexBehavior),
            new UIPropertyMetadata(new PropertyChangedCallback((sender, args) => {
                AttachDayIndexBehavior behavior = sender as AttachDayIndexBehavior;
                //behavior.AssociatedObject
                
            })));

        private void MarkIndex()
        {
            if (AssociatedObject.Items.Count == 0 || AssociatedObject.ItemContainerGenerator.ContainerFromIndex(0) == null) return;
            for (int i = 1; i < AssociatedObject.Items.Count+1; i++)
            {
                
                DependencyObject viewItem = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(i);
                var element = FindVisualChild<CheckBox>(viewItem);
                if (element != null)
                {
                    element.SetValue(FrameworkElement.TagProperty, string.Format("{0:00}", i));
                    if (this.Resources != null)
                        element.SetValue(ContentControl.ContentProperty, Resources[i]);
                }
            }
            AssociatedObject.Loaded -= AssociatedObject_Loaded;
        }

        public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}
